package com.idealighter.game.robot.common.cards;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 欢乐五张牌型计算器 .
 */
public class HappyFiveCompare extends CardCompare {

  public HappyFiveCompare(CardType handPowerType, List<CardNumber> tieBreakingInformation,
      List<Card> cardList) {
    super(handPowerType, tieBreakingInformation, cardList);
  }

  @Override
  public int compareTo(CardCompare other) {
    int typeDifference = handPowerType.getPower() - other.handPowerType.getPower();
    if (typeDifference == 0) {
      for (int i = 0; i < tieBreakingInformation.size(); i++) {
        if (other.tieBreakingInformation.size() <= i) {
          return 0;
        }
        int tieDifference = tieBreakingInformation.get(i).getPower()
            - other.tieBreakingInformation.get(i).getPower();
        if (tieDifference != 0) {
          return tieDifference;
        }
      }
      return compareSuit(tieBreakingInformation.get(tieBreakingInformation.size() - 1), cardList)
          - compareSuit(other.tieBreakingInformation.get(other.tieBreakingInformation.size() - 1),
              other.cardList);
    }

    return typeDifference;
  }

  /**
   * 比较花色.
   *
   * @return .
   */
  public int compareSuit(CardNumber cardNumber, List<Card> cardLists) {
    List<CardSuit> suit = Lists.newArrayList();
    suit.addAll(cardLists.stream().filter(card -> card.getNumber() == cardNumber).map(Card::getSuit)
        .collect(Collectors.toList()));
    return getMaxSuit(suit);
  }

  /**
   * 获取最大的花色.
   *
   * @param cardSuitList .
   * @return .
   */
  public int getMaxSuit(List<CardSuit> cardSuitList) {
    int suitPower = 0;
    for (CardSuit suit : cardSuitList) {
      if (suit.getPower() >= suitPower) {
        suitPower = suit.getPower();
      }
    }
    return suitPower;
  }

}
